package com.wxq.koland.xiaoliscan;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import com.wxq.koland.xiaoliscan.utils.ActivityUtil;
import com.wxq.koland.xiaoliscan.utils.LogUtil;
import com.wxq.koland.xiaoliscan.utils.SharedPreferenceUtil;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by wxq on 2019/2/12.
 * Love Life ~
 */

public class MyApp extends Application{

    private static MyApp singleton ;
    public static Context appContext ;
    public static final int TIMEOUT = 60;
    private static OkHttpClient mOkHttpClient;


    public static MyApp getInstance(){
        return singleton ;
    }

    @Override
    public void onCreate () {
        super.onCreate();
        singleton = this ;
        appContext = getApplicationContext() ;
        
        initOKHttp();
        initLogger();
    }

    private void initLogger () {
        LogUtil.initLog(true);
        SharedPreferenceUtil.init(this);
    }

    /**
     * 杀进程
     */
    public void onLowMemory() {
        super.onLowMemory();
        ActivityUtil.getInstance().exitSystem();
        System.gc();
    }

    /**
     * 配置改变的时候监听
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged (Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 全局httpclient
     *
     * @return
     */
    public static OkHttpClient initOKHttp() {
        if (mOkHttpClient == null) {
            mOkHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
                    .readTimeout(TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
                    .writeTimeout(TIMEOUT, TimeUnit.SECONDS)//设置写入超时时间
//                    .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
//                    //cookie
//                    .addInterceptor(new CookieReadInterceptor())
//                    .addInterceptor(new CookiesSaveInterceptor())
                    .build();
        }
        return mOkHttpClient;
    }



}
